// pages/common/test.js
var counteventtimes = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memberimgUrls: [{
        "id": 1,
        "name": "1111",
        "list": [{
          "start_time": "1590064200",
          "end_time": "1590150600",
          "status": "going"
        }]
      },
      {
        "id": 5,
        "name": "3332",
        "desc": "444",
        "list": [{
          "start_time": "1590064200",
          "end_time": "1590150600",
          "status": "going"
        }, {
          "start_time": "1590060600",
          "end_time": "1590147000",
          "status": "going"
        }, {
          "start_time": "1590033600",
          "end_time": "1590638400",
          "status": "going"
        }, {
          "start_time": "1590134400",
          "end_time": "1590220800",
          "status": "going"
        }]
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onHide: function () {
    console.log('onhide')
    for (var i = 0; i < this.data.memberimgUrls.length; i++) {
      for (var j = 0; j < this.data.memberimgUrls[i].list.length; j++) {
        clearTimeout(counteventtimes[this.data.memberimgUrls[i].id][j]);
        counteventtimes[this.data.memberimgUrls[i].id][j] = 0
      }
    }
    console.log('全部停止了', JSON.stringify(counteventtimes))
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  goToIndex(){
    wx.switchTab({
      url: '../index/index',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   for (var i = 0; i < this.data.memberimgUrls.length; i++) {
      if (this.data.memberimgUrls[i].list.length > 0) {
        for (var j = 0; j < this.data.memberimgUrls[i].list.length; j++) {
          this.data.memberimgUrls[i].list[j].counttime = '00:00:00';
          if (this.data.memberimgUrls[i].list[j].status == 'going') {
            this.countEventTime(i, j)
          }
        }
      }
    }
  },
  countEventTime(frindex, secindex) {
    console.log('counttime',2121)
    var that = this;
    var stime = Date.parse(new Date()) / 1000,
      id = this.data.memberimgUrls[frindex].id,
      endtime = this.data.memberimgUrls[frindex].list[secindex].end_time,
      status = this.data.memberimgUrls[frindex].list[secindex].status,
      frindex = frindex,
      secindex = secindex;
    var leftTime = endtime - stime;
    var d, h, m, s;
    d = Math.floor(leftTime / 60 / 60 / 24);
    !counteventtimes[id] && (counteventtimes[id] = [])
    counteventtimes[id][secindex] && clearTimeout(counteventtimes[id][secindex])
    if (leftTime > 0) {
      h = d * 24 + Math.floor(leftTime / 60 / 60 % 24);
      m = Math.floor(leftTime / 60 % 60);
      s = Math.floor(leftTime % 60);
      this.data.memberimgUrls[frindex].list[secindex].percent = (((Date.parse(new Date()) / 1000 - this.data.memberimgUrls[frindex].list[secindex].start_time) / (this.data.memberimgUrls[frindex].list[secindex].end_time - this.data.memberimgUrls[frindex].list[secindex].start_time)).toFixed(2)) * 100;
      this.setData({
        memberimgUrls: this.data.memberimgUrls
      })
      counteventtimes[id][secindex] = setTimeout(function () {
        that.countEventTime(frindex, secindex);
      }, 1000);
    } else if (leftTime <= 0) {
      if (status == 'going') {
        h = 0;
        m = 0;
        s = 0;
      } else {
        h = 0;
        m = 0;
        s = 0;
      }
    }
    h = checkTime(h);
    m = checkTime(m);
    s = checkTime(s);

    function checkTime(i) {
      if (i < 10) {
        i = "0" + i;
      }
      return i;
    }
    if (d <= 0) {
      this.data.memberimgUrls[frindex].list[secindex].counttime = h + ':' + m + ':' + s;
      this.setData({
        memberimgUrls: this.data.memberimgUrls
      })
    } else {
      this.data.memberimgUrls[frindex].list[secindex].counttime = d + '天';
      this.setData({
        memberimgUrls: this.data.memberimgUrls
      })
    }
     
  },
  /**
   * 生命周期函数--监听页面隐藏
   */


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
console.log('onunload')
for (var i = 0; i < this.data.memberimgUrls.length; i++) {
  for (var j = 0; j < this.data.memberimgUrls[i].list.length; j++) {
    clearTimeout(counteventtimes[this.data.memberimgUrls[i].id][j]);
    counteventtimes[this.data.memberimgUrls[i].id][j] = 0
  }
}
console.log('全部停止了', JSON.stringify(counteventtimes))
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})